//
//  FEWebViewController.m
//  familyeducation
//
//  Created by 聂志康 on 25/02/2017.
//  Copyright © 2017 shangdingkeji. All rights reserved.
//

#import "FEWebViewController.h"
#import <WebKit/WebKit.h>

@interface FEWebViewController () <WKNavigationDelegate,WKUIDelegate>
@property (nonatomic, strong) WKWebView *webView;
@property (nonatomic, strong) UIView *progressView;
@property (nonatomic, strong) UIView *progressViewProgress;
@end

@implementation FEWebViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    WKWebView *webView = [[WKWebView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight-SafeAreaTopHeight)];
    webView.backgroundColor = [UIColor whiteColor];
    webView.navigationDelegate = self;
    webView.UIDelegate = self;
    self.webView = webView;
    [self.view addSubview:self.webView];
//    [self.view addSubview:self.progressView];
    if (![self.UrlString isEqualToString:@""] && self.UrlString) {
        [self.webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:self.UrlString]]];
    } else if (![self.HtmlString isEqualToString:@""] && self.HtmlString) {
        [self.webView loadHTMLString:self.HtmlString baseURL:nil];
    }
    [self.webView addObserver:self forKeyPath:@"estimatedProgress" options:NSKeyValueObservingOptionNew context:nil];
}

-(UIView *)progressView
{
    if (!_progressView) {
        _progressView = [[UIProgressView alloc] initWithFrame:CGRectMake(0, 0, [[UIScreen mainScreen] bounds].size.width, 3)];
        _progressView.backgroundColor = [UIColor lightGrayColor];
        UIView *progressViewProgress = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 1, 2)];
        self.progressViewProgress = progressViewProgress;
        [_progressView addSubview:progressViewProgress];
        progressViewProgress.backgroundColor = hexColor(2F7CFB);
    }
    return _progressView;
}

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSString *,id> *)change context:(void *)context {
    if ([keyPath isEqualToString:@"estimatedProgress"]) {
        [UIView animateWithDuration:0.25 animations:^{
            self.progressViewProgress.frame = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width*self.webView.estimatedProgress, 2);
        }];
        if (self.webView.estimatedProgress == 1) {
            /*
             *添加一个简单的动画，将progressView的Height变为1.4倍，在开始加载网页的代理中会恢复为1.5倍
             *动画时长0.25s，延时0.3s后开始动画
             *动画结束后将progressView隐藏
             */
            self.progressView.hidden = YES;
            self.progressViewProgress.hidden = YES;
        }
    }else{
        [super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
    }
}

//开始加载
- (void)webView:(WKWebView *)webView didStartProvisionalNavigation:(WKNavigation *)navigation {
    NSLog(@"开始加载网页");
//    //开始加载网页时展示出progressView
//    self.progressView.hidden = NO;
//    //防止progressView被网页挡住
//    [self.view bringSubviewToFront:self.progressView];
}

//加载完成
- (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation {
    NSLog(@"加载完成");
//    //加载完成后隐藏progressView
//    self.progressView.hidden = YES;
//    self.progressViewProgress.hidden = YES;
//    //获取所有的html
////    NSString *allHtml = @"document.documentElement.innerHTML";
//    //获取网页title
//    NSString *htmlTitle = @"document.title";
//    //获取网页的一个值
////    NSString *htmlNum = @"document.getElementById('title').innerText";
//    //获取到得网页内容
//    if (!self.navigationItem.title || [self.navigationItem.title isEqualToString:@""]) {
//        [webView evaluateJavaScript:htmlTitle completionHandler:^(id _Nullable result, NSError * _Nullable error) {
//            self.navigationItem.title = result;
//        }];
//    }
}

//加载失败
- (void)webView:(WKWebView *)webView didFailProvisionalNavigation:(WKNavigation *)navigation withError:(NSError *)error {
    NSLog(@"加载失败");
    //加载失败同样需要隐藏progressView
    self.progressView.hidden = YES;
    self.progressViewProgress.hidden = YES;
}

- (void)dealloc {
    [self.webView removeObserver:self forKeyPath:@"estimatedProgress"];
}

// 在收到响应后，决定是否跳转
- (void)webView:(WKWebView *)webView decidePolicyForNavigationResponse:(WKNavigationResponse *)navigationResponse decisionHandler:(void (^)(WKNavigationResponsePolicy))decisionHandler {
    decisionHandler(WKNavigationResponsePolicyAllow);
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
